package com.teachoa.bean;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.teachoa.constant.Global;

public class Msg extends Model<Msg> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7067890396567391449L;
	public static final Msg dao = new Msg();
	
	public Page<Msg> findMsgsByPage(int pageNo, int isProMsg) {
		return dao.paginate(pageNo, Global.pageSize, "select *"
				, "from msg where is_promsg = ? order by create_time desc", isProMsg);
	}
	
	public void publish(String id) {
		Msg msg = dao.findById(id);
		msg.set("status", 1);
		msg.update();
	}
	
	public List<Msg> listTopSysMsgs() {
		return dao.find("select * from msg where type = 1 and is_promsg = 0 order by create_time limit 0,4 ");
	}
	
	public List<Msg> listOwnMsgs(String userId) {
		return dao.find("select a.* from msg a left join user_msg b on a.id = b.msg_id "
				+ "where b.user_id = ? order by a.create_time limit 0, 5", userId);
	}
	
	public Page<Msg> findMsgsByPage(int pageNo, String userId) {
		return dao.paginate(pageNo, Global.pageSize, "select a.*"
				, "from msg a left join user_msg b on a.id = b.msg_id "
				+ "where b.user_id = ? order by a.create_time", userId);
	}
	
}
